<?php

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Mod_Type extends CI_Model
{ 
  public function getType($start, $num)
  {
    $this->db->select('c.*, p.'.field('name').' AS '.field('category'))
        ->from(table('type').' c')
        ->join(table('category').' p', 'c.'.field('categoryId').'= p.'.field('id'))
        ->order_by('c.'.field('updatedAt'), 'DESC');

    $this->db->limit($num, $start);

    return $this->db->get();
  }
  
  public function getTypeNum()
  {
    $objDb = $this->getAllType();
    return $objDb->num_rows();
  }
  
  public function getAllType() {
    $this->db->select('c.*, p.'.field('name').' AS '.field('category'))
        ->from(table('type').' c')
        ->join(table('category').' p', 'c.'.field('categoryId').'= p.'.field('id'))
        ->order_by('c.'.field('updatedAt'), 'DESC');

    return $this->db->get();
  }
  
  public function getTypeById($id)
  {
    $this->db->select('c.*, p.'.field('name').' AS '.field('category'))
        ->from(table('type').' c')
        ->join(table('category').' p', 'c.'.field('categoryId').'= p.'.field('id'))
        ->where('c.'.field('id'), $id);

    $result = $this->db->get();

    return $result->row_array();
  }
  
  public function getCategoryList($id = NULL)
  {
    $this->db->select(field('id') . ', ' . field('name'))
        ->from(table('category'))
        ->where(field('isDisable'), 0);

    if(!empty($id))
      $this->db->where(field('id').' !=', $id);
    $this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0){
      foreach($result->result_array() as $row){
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function insert()
  {
    $data = array(
      field('name')       => $this->input->post('name'), 
      field('categoryId') => $this->input->post('category'),
      field('description')=> $this->input->post('description')
    );
    
    $this->db->insert(table('type'), $data); 
    return $this->db->affected_rows();
  }
  
  public function update($id)
  {
    $disable  = $this->input->post('disable', false);
    $data = array(
      field('name')       => $this->input->post('name'), 
      field('categoryId') => $this->input->post('category'),
      field('description')=> $this->input->post('description'),
      field('isDisable')  => ($disable == 1 ? 1 : 0)
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
    
    $this->db->where(field('id'),$id);
    $this->db->update(table('type'), $data);

    return $this->db->affected_rows();
  }
  
  public function delete($id)
  {
    $this->db->where(field('id'), $id);
    $this->db->delete(table('type'));

    return $this->db->affected_rows();
  }
  /*
  public function isTypeUsingInType($id)
  {
    $this->db->from(table('type'))
        ->where(field('categoryId'), $id);
    
    if($this->db->count_all_results() > 0)
    {
      return TRUE;  
    }
    
    return FALSE;
  }
  
  public function isTypeUsingInBook($id)
  {
    $this->db->from(table('book'))
        ->where(field('typeId'), $id);
    
    if($this->db->count_all_results() > 0)
    {
      return TRUE;  
    }
    
    return FALSE;
  }*/
}